/*
 * 
 * LegendShop 多用户商城系统
 * 
 *  版权所有,并保留所有权利。
 * 
 */
package com.legendshop.oa.dao.impl;

import com.legendshop.dao.impl.GenericDaoImpl;
import com.legendshop.dao.support.EntityCriterion;
import com.legendshop.dao.support.PageSupport;
import com.legendshop.dao.support.QueryMap;
import com.legendshop.dao.support.SimpleSqlQuery;
import com.legendshop.oa.dao.WeekLogDao;
import com.legendshop.oa.dto.WeekLogReport;
import com.legendshop.oa.dto.WeeklyTimeDto;
import com.legendshop.oa.model.WeekLog;
import com.legendshop.util.WeeklyTimeUtil;
import com.legendshop.dao.sql.ConfigCode;

import java.util.Date;
import java.util.List;

/**
 * The Class WeekLogDaoImpl.
  * 周报Dao实现类
 */

public class WeekLogDaoImpl extends GenericDaoImpl<WeekLog, Long> implements WeekLogDao  {

	private String getUserWeekLogListSQL = "select t.project_id as projectId, p.name as projectName, sum(t.consumed) as consumed from ls_taskestimate t, ls_project p " +
			"where t.project_id = p.id and t.user_name = ? and t.date >= ? and t.date < ? group by t.project_id";
     
	/**
	 *  根据商城获取周报列表
	 */     
    public List<WeekLog> getWeekLog(String userName){
   		return this.queryByProperties(new EntityCriterion().eq("userName", userName));
    }
    
   	/**
	 *  根据Id获取周报
	 */
	public WeekLog getWeekLog(Long id){
		return getById(id);
	}

   /**
	 *  删除周报
	 */	
    public int deleteWeekLog(WeekLog weekLog){
    	return delete(weekLog);
    }

   /**
	 *  保存周报
	 */		
	public Long saveWeekLog(WeekLog weekLog){
		return save(weekLog);
	}

   /**
	 *  更新周报
	 */		
	public int updateWeekLog(WeekLog weekLog){
		return update(weekLog);
	}

   /**
	 *  查询周报列表
	 */		
	@Override
	public PageSupport<WeekLog> getWeekLogByLeaderUserNameDate(String leaderName, String curPageNO, String name, String realName, Date startDate, Date endDate) {
		QueryMap map = new QueryMap();
		map.put("leaderName",leaderName);
		map.put("name", name);
		map.like("realName", realName);
		map.put("startDate", startDate);
		map.put("endDate", endDate);
		String querySql = ConfigCode.getInstance().getCode("oa.getWeeklyLog", map);
		String sqlCount = ConfigCode.getInstance().getCode("oa.getWeeklyLogCount", map);
		
		SimpleSqlQuery query = new SimpleSqlQuery(WeekLog.class,curPageNO);
		
		query.setAllCountString(sqlCount);
		query.setQueryString(querySql);
		query.setParam(map.toArray());
		return querySimplePage(query);
	}

	@Override
	public WeekLog getWeekLog(String userName, Integer year, Integer weekNo) {
		return get("SELECT * FROM ls_week_log WHERE user_name = ? AND YEAR = ? AND week_no = ?", WeekLog.class, userName, year, weekNo);
	}

	/**
	 * 获取特定用户的周报
	 */
	@Override
	public PageSupport<WeekLog> getWeekLogByUserYearWeek(boolean isAdmin, String leaderName, String curPageNO, String userName, Integer year, Integer weekNo) {
		QueryMap map = new QueryMap();
		if(!isAdmin){
			map.put("leaderName", leaderName);
		}
		map.put("name", userName);
		if(year != null && year != 0 && weekNo != null && weekNo != 0) {//页面用0来表示不传值
			map.put("year", year);
			map.put("weekNo", weekNo);
		}

		String sqlId;
		String sqlConutSQL;
		if(!isAdmin){//一般用户
			sqlId = "oa.getWeeklyLog";
			sqlConutSQL = "oa.getWeeklyLogCount";
		}else{//管理员的SQL
			sqlId = "oa.getWeeklyLogAdmin";
			sqlConutSQL = "oa.getWeeklyLogAdminCount";
		}


		String querySql = ConfigCode.getInstance().getCode(sqlId, map);
		String sqlCount = ConfigCode.getInstance().getCode(sqlConutSQL, map);
		
		SimpleSqlQuery query = new SimpleSqlQuery(WeekLog.class,curPageNO);
		query.setPageSize(50);
		query.setAllCountString(sqlCount);
		query.setQueryString(querySql);
		query.setParam(map.toArray());
		return querySimplePage(query);
	}
	
	/**
	 * 获取所有用户的某周的周报
	 */
	@Override
	public PageSupport<WeekLog> getWeekLogByLeaderYearWeek(boolean isAdmin, String leaderName, String curPageNO, Integer year, Integer weekNo) {
		QueryMap map = new QueryMap();
		if(!isAdmin){
			map.put("leaderName", leaderName);
		}

		if(year != null && year != 0){
			map.put("year", year);
		}
		if(weekNo != null && weekNo != 0){
			map.put("weekNo", weekNo);
		}

		String sqlId;
		String sqlConutSQL;
		if(!isAdmin){//一般用户
			sqlId = "oa.getWeeklyLogByWeek";
			sqlConutSQL = "oa.getWeeklyLogCountByWeek";
		}else{//管理员的SQL
			sqlId = "oa.getAdminWeeklyLogByWeek";
			sqlConutSQL = "oa.getAdminWeeklyLogCountByWeek";
		}
		String querySql = ConfigCode.getInstance().getCode(sqlId, map);
		String sqlCount = ConfigCode.getInstance().getCode(sqlConutSQL, map);
		
		SimpleSqlQuery query = new SimpleSqlQuery(WeekLog.class,curPageNO);
		query.setPageSize(50);
		query.setAllCountString(sqlCount);
		query.setQueryString(querySql);
		query.setParam(map.toArray());
		return querySimplePage(query);
	}


	/**
	 * 是否是我的下级
	 */
	@Override
	public boolean isMycolleague(String leaderName, String userName) {
		return get("select 1 from ls_admin_user where FIND_IN_SET(name, getChildList(?)) and name = ?", Integer.class, leaderName, userName)  != null;
	}


	@Override
	public List<WeekLogReport> getWeekLogReport(WeekLog weekLog) {
		WeeklyTimeDto weeklyTimeDto = WeeklyTimeUtil.getWeeklyTime(weekLog.getYear(), weekLog.getWeekNo());
		return query(getUserWeekLogListSQL, WeekLogReport.class, weekLog.getUserName(), weeklyTimeDto.getStartDate(), weeklyTimeDto.getEndDate());
	}

}
